<?php

/*
 * Table/ProfilTable.php
 */

namespace RHX\Model\Table;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;

use RHX\Model\Object\Profil;

class ProfilTable extends AbstractTableGateway {

    protected $table = 'rh_profil';

    public function __construct(Adapter $adapter) {
	$this->adapter			 = $adapter;
	$this->resultSetPrototype	 = new ResultSet();
	$this->resultSetPrototype->setArrayObjectPrototype(new Profil());
	$this->initialize();
    }

    public function getAll() {
	return $this->select();
    }

    public function getSelectList() {
	$rows			 = $this->select();
	$arr			 = array();
	foreach ($rows as $profil)
	    $arr[$profil->id]	 = $profil->libelle;

	return $arr;
    }

    public function get($id) {
	$id	 = (int) $id;
	$row	 = $this->select(array('id' => $id))
		->current();
	if (!$row)
	    throw new \Exception("Impossible de trouver #$id");

	return $row;
    }

    public function save(Profil $profil) {
	$data	 = array(
	    'libelle'		 => $profil->libelle,
	    'employe_data'		 => $profil->employe_data,
	    'employe_planning'	 => $profil->employe_planning,
	    'manager_data'		 => $profil->manager_data,
	    'manager_planning'	 => $profil->manager_planning,
	    'site_access'		 => $profil->site_access,
	    'description'		 => $profil->description
	);
	$id	 = (int) $profil->id;
	if ($id == 0)
	    $this->insert($data);
	elseif ($this->get($id))
	    $this->update($data, array('id' => $id));
	else
	    throw new \Exception("Impossible de trouver #$id");

	if ($id == 0)
	    return $this->lastInsertValue;
	else
	    return $id;
    }

    /**
     * Effacer un profil
     *
     * @param int $id
     * @return int nombre de profil effacé
     */
    public function erase($id) {
	return $this->delete(array('id' => $id));
    }

}

?>
